/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package com.icerhe.trigger.task;

import org.json.JSONException;

import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;

import com.icerhe.trigger.Consts;
import com.icerhe.trigger.R;
import com.icerhe.trigger.RingtonService;
import com.icerhe.trigger.TaskWakeLock;
import com.icerhe.trigger.task.CronTask.Columns;

/**
 * 报时提醒类
 * @author icer
 */
public class AlarmTask extends CronTask {
	
	public static final int NOTIFICATION_TYPE = 1;
	public static final String SNOOZE_ID = "SNOOZE_ID";
	public static final String SNOOZE_ACTION = "com.icerhe.autotask.SNOOZE_ACTION";
	
	public static final String RINGTONE_ENABLED = "ringtone_enabled";//是否启用铃声
	public static final String VIBRATOR_ENABLED = "vibrator_enabled";//是否启用振动
	public static final String USE_MUSIC = "use_music";//是否使用自选音乐取代系统铃声
	public static final String RINGTONE_PATH = "ringtone_path";//铃声路径
	public static final String MUSIC_PATH = "music_path";//自选音乐路径
	public static final String SNOOZE_DURATION = "snooze_duration";//自选音乐路径
	public static final int    DEFAULT_SNOOZE_DURATION = 10;
	
	public AlarmTask(Context context){
		super();
		title = context.getString(R.string.default_alarm_label);
		parse(context.getText(R.string.alarm_default_cronpatten).toString());
	}
	
	public AlarmTask(Cursor c) {
		super(c);	
	}

	@Override
	public void runTask(Context context) {
		Log.d(Consts.TAG, "AlarmTask runTask() start ID:"+id);
		//hold the CPU wake lock
		TaskWakeLock.acquireWakeLock(context);
        // Close dialogs and window shade
        Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(closeDialogs);
        
        KeyguardManager km = (KeyguardManager)context.getSystemService(
        										Context.KEYGUARD_SERVICE);
        
        //如果处在锁定状态,使用AlarmTaskActivity,否则使用AlarmTaskDialog
        Intent alarmIntent = km.inKeyguardRestrictedInputMode()?
        			new Intent(context, AlarmTaskActivity.class):
        			new Intent(context, AlarmTaskDialog.class);
        alarmIntent.putExtra(INTENT_TASK_ID, id);
        
        alarmIntent.setFlags(
        		Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
        
        context.startActivity(alarmIntent);
        
        //启动RingtonService,播放铃声和振动
        Intent ringtonIntent = new Intent(context, RingtonService.class);
        ringtonIntent.putExtra(RingtonService.RINGTON_ENABLED, isRingtonEnabled());
        ringtonIntent.putExtra(RingtonService.VIBRATOR_ENABLED, isVibratorEnabled());
        ringtonIntent.putExtra(RingtonService.RINGTON_URI, isUseMusic()?getMusicPath():getRingtonePath());
        context.startService(ringtonIntent);
        
        //设置Notification
        Intent notify = new Intent(context, AlarmTaskDialog.class);
        notify.putExtra(CronTask.Columns.ID, id);
        PendingIntent pendingNototify = PendingIntent.getActivity(context, id, notify, 0);
        Notification n = new Notification(
        		R.drawable.stat_notify_alarm, 
        		title, 
        		System.currentTimeMillis());
        n.setLatestEventInfo(context, title, description, pendingNototify);
        n.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONGOING_EVENT;
        n.defaults |= Notification.DEFAULT_LIGHTS;
        
        NotificationManager nm = 
        	(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_TYPE, n);
        
	}
	
	public boolean isRingtonEnabled() {
		try {
			return extra.has(RINGTONE_ENABLED)?extra.getBoolean(RINGTONE_ENABLED):true;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: ringtone_enabled");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return true;
	}

	public void setRingtonEnabled(boolean ringtonEnabled) {
		try {
			extra.put(RINGTONE_ENABLED, ringtonEnabled);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: ringtone_enabled");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

	public boolean isVibratorEnabled() {
		try {
			return extra.has(VIBRATOR_ENABLED)?extra.getBoolean(VIBRATOR_ENABLED):true;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: vibrator_enabled");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return true;
	}

	public void setVibratorEnabled(boolean vibratorEnabled) {
		try {
			extra.put(VIBRATOR_ENABLED, vibratorEnabled);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: vibrator_enabled");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

	public boolean isUseMusic() {
		try {
			return extra.has(USE_MUSIC)?extra.getBoolean(USE_MUSIC):false;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: use_music");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return true;
	}

	public void setUseMusic(boolean useMusic) {
		try {
			extra.put(USE_MUSIC, useMusic);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: use_music");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

	public String getRingtonePath() {
		try {
			return extra.has(RINGTONE_PATH)?extra.getString(RINGTONE_PATH):null;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: ringtone_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return null;
	}

	public void setRingtonePath(String ringtonePath) {
		try {
			extra.put(RINGTONE_PATH, ringtonePath);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: ringtone_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

	public String getMusicPath() {
		try {
			return extra.has(MUSIC_PATH)?extra.getString(MUSIC_PATH):null;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: music_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return null;
	}

	public void setMusicPath(String musicPath) {
		try {
			extra.put(MUSIC_PATH, musicPath);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: music_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}
	
	public int getSnoozeDuration() {
		try {
			return extra.has(SNOOZE_DURATION)?extra.getInt(SNOOZE_DURATION):DEFAULT_SNOOZE_DURATION;
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to get value: music_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}	
		return DEFAULT_SNOOZE_DURATION;
	}
	
	public void setSnoozeDuration(int snoozeDuration) {
		try {
			extra.put(SNOOZE_DURATION, snoozeDuration);
		} catch (JSONException e) {
			Log.e(Consts.TAG, "Fail to set value: music_path");
			Log.e(Consts.TAG, Log.getStackTraceString(e));
		}
	}

}
